package com.wildlife.springboot.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wildlife.springboot.common.Constants;
import com.wildlife.springboot.common.Result;
import com.wildlife.springboot.controller.dto.ConsumerDTO;
import com.wildlife.springboot.entity.Consumer;
import com.wildlife.springboot.service.IConsumerService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Resource
    private IConsumerService consumerService;

    @PostMapping("/login")
    public boolean login(@RequestBody ConsumerDTO consumerDTO){
        String username = consumerDTO.getUsername();
        String password = consumerDTO.getPassword();
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)){
//            return Result.error(Constants.CODE_400, "参数错误");
            return false;
        }
//        ConsumerDTO dto = consumerService.login(consumerDTO);
        return consumerService.login(consumerDTO);
    }

//    @PostMapping("/register")
//    public Result register(@RequestBody ConsumerDTO consumerDTO){
//        String username = consumerDTO.getUsername();
//        String password = consumerDTO.getPassword();
//        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)){
//            return Result.error(Constants.CODE_400, "参数错误");
//        }
//        return Result.success(consumerService.register(consumerDTO));
//    }

    //新增和修改
    @PostMapping
    public Result save(@RequestBody Consumer consumer){
        //新增或更新
        return Result.success(consumerService.saveOrUpdate(consumer));
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(consumerService.removeById(id));
    }

    @PostMapping ("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        return Result.success(consumerService.removeByIds(ids));
    }

    @GetMapping
    public Result findAll() {
        return Result.success(consumerService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(consumerService.getById(id));
    }

    @GetMapping("/username/{username}")
    public Result findOne(@PathVariable String username) {
        QueryWrapper<Consumer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return Result.success(consumerService.getOne(queryWrapper));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String username
    ) {
        QueryWrapper<Consumer> queryWrapper = new QueryWrapper<>();
        if (!"".equals(username)) {
            queryWrapper.like("username", username);
        }
//        if (!"".equals(email)) {
//            queryWrapper.like("email", email);
//        }
//        if (!"".equals(address)) {
//            queryWrapper.like("address", address);
//        }
        return Result.success(consumerService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}
